package 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import newcommerce.lastfm.api.clients.ArtistClient;
	import newcommerce.lastfm.api.data.LFMAlbumData;
	import newcommerce.lastfm.api.data.LFMArtistData;
	import newcommerce.lastfm.api.data.LFMTopTrackData;
	import newcommerce.lastfm.api.events.LFMArtistInfoEvent;
	import newcommerce.lastfm.api.events.LFMTagFeedEvent;
	import newcommerce.lastfm.api.events.LFMTopAlbumsEvent;
	import newcommerce.lastfm.api.events.LFMTopTrackEvent;
	import newcommerce.lastfm.api.feeds.LFMAlbumFeed;
	import newcommerce.lastfm.api.feeds.LFMTrackFeed;
	import newcommerce.lastfm.api.LastFMClient;
	
	/**
	 * ...
	 * @author Martin Legris ( http://blog.martinlegris.com )
	 */
	public class Main extends Sprite 
	{
		private var _artistClient:ArtistClient;		
		private var _client:LastFMClient;
		
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
			
			_client = LastFMClient.getInstance();
			_artistClient = _client.artist;
			_artistClient.addEventListener(LFMTopAlbumsEvent.FEED_READY, doTopAlbumsReady);
			_artistClient.addEventListener(LFMTopTrackEvent.FEED_READY, doTopTracksReady);
			_artistClient.addEventListener(LFMArtistInfoEvent.DATA_READY, doArtistDataReady);
			
			_artistClient.getTopAlbums("Rise Against");
			_artistClient.getTopTracks("Rise Against");
			_artistClient.getInfo("Rise Against");
		}
		
		private function doTopAlbumsReady(evt:LFMTopAlbumsEvent):void
		{
			trace("doTopAlbumsReady");
			
			var albums:Array = [];
			var feed:LFMAlbumFeed = evt.feed;
			var albumData:LFMAlbumData;
			var i:Number = 0;
			for (var i:Number = 0; i < Math.min(4, feed.count); i++)
			{
				albumData = feed.next();
				// album name
				var albumName:String = albumData.name;
				// image
				var albumUrl:String = albumData.images.getMedium().url;
				trace("album["+i+"] Name[" + albumName + "]");
				trace("album[" + i + "] Url[" + albumUrl + "]");
				
				albums.push( { name:albumName, url:albumUrl } );
			}
			
			// albums contains the top tracks
		}		
		
		private function doTopTracksReady(evt:LFMTopTrackEvent):void
		{
			var tracks:Array = [];
			var feed:LFMTrackFeed = evt.feed;
			var track:LFMTopTrackData;
			for (var i:Number = 0; i < Math.min(5, feed.count); i++)
			{
				track = feed.next();
				tracks.push(track.name);
				trace("track[" + i + "] name[" + tracks[tracks.length - 1] + "]");
			}
			
			// tracks contains the top tracks
		}
		
		private function doArtistDataReady(evt:LFMArtistInfoEvent):void
		{
			var bio:String = evt.artistInfo.bio.summary;
			trace("bio:" + bio);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
		}
		
	}
	
}